<?php
// 謨ｰ謐ｮ蠎謎ｸｭ髣ｴ螻ょｮ樒鴫邀ｻ
// 謾ｯ謖［ysql
class DB
{
	static private $_instances = array();
	
    // 謨ｰ謐ｮ蠎馴�鄂ｮ譬�ｯ�
	protected $DBConfigKey = null;
    // 謨ｰ謐ｮ蠎楢ｿ樊磁蜿よ焚
    protected $config = null;
    
    // 譏ｯ蜷ｦ蟾ｲ扈剰ｿ樊磁謨ｰ謐ｮ蠎�
    protected $bConnected = false;

    // 蠖灘燕霑樊磁譬�ｯ�
    protected $ptrLink = null;
    // 蠖灘燕譟･隸｢譬�ｯ�
    protected $ptrQuery = null;
    
    // 蠖灘燕SQL謖�ｻ､
    protected $sqlQuery = '';
    
    // 髞呵ｯｯ菫｡諱ｯ
    protected $error = '';
    // 譏ｯ蜷ｦ譏ｾ遉ｺ隹�ｯ穂ｿ｡諱ｯ 螯よ棡蜷ｯ逕ｨ莨壼惠譌･蠢玲枚莉ｶ隶ｰ蠖不ql隸ｭ蜿･
    public $debug = false;
    
    
    public function __construct( $DBConfigKey ) 
    {
        //if ( !extension_loaded('mysql') ) {
            //exception
        //}
        $this->_parseConfig( $DBConfigKey );
        $this->DBConfigKey = $DBConfigKey;
    }
    
    public function __destruct() {
        // 蜈ｳ髣ｭ霑樊磁
        $this->close();
    }
    
    protected function _parseConfig( $DBConfigKey )
    {
    	$NewDBConfig = config( $DBConfigKey );
    	if ( !isset( $NewDBConfig ) ) {
    		//excption
    	}
    	
    	$this->config = $NewDBConfig;
    }
    
    public function error()
    {
    	$this->error = mysql_error( $this->ptrLink );
    	if ( $this->sqlQuery != '' ) {
    		$this->error .= '\n => ' . $this->sqlQuery; 
    	}
    	return $this->error;
    }
    
    protected function debug() 
    {
        // 隶ｰ蠖墓桃菴懈慮髣ｴ�瑚ｯｭ蜿･蜿顔ｻ捺棡
    }
    
   	// SQL謖�ｻ､螳牙�霑�ｻ､
	function escapeString( $str )
	{
		return mysql_escape_string( $str );
	}
	
	function escapeBlobString( $str )
	{
		return mysql_real_escape_string( $str, $this->$ptrLink );
	}
    
    public function connect()
    {
    	if( !$this->bConnected ) {
    		// 霑樊磁謖�ｮ嗄ysql
    		$this->ptrLink = mysql_pconnect( $this->config['hostname'], $this->config['username'], $this->config['password'] );
    		if ( !$this->$ptrLink ) {
                // exception error()
            }
            // 騾画叫謖�ｮ嗄ysql謨ｰ謐ｮ蠎�
            if ( ( !empty( $config['database'] ) && !mysql_select_db( $this->config['database'], $this->ptrLink) ) ) {
            	// exception error()
            }
            
            mysql_query("SET NAMES '".config('DB_CHARSET')."'", $this->linkID);
            
            $this->bConnected = true;
    	}
    }
    
    public function free()
    {
    	if ( !empty( $this->ptrQuery ) ) {
    		mysql_free_result( $this->ptrQuery );
    	}
    }
    
    public function close()
    {
        $this->free();
        
        if ( $this->ptrLink && !mysql_close( $this->ptrLink ) ) {
            // exception error()
        }
        $this->ptrLink = null;
        $this->bConnected = false;
    }
    
    public function query( $sql = '' )
    {
        if ( !$this->ptrLink ) {
        	return false;
        }
        
        //驥頑叛蜑肴ｬ｡逧�衍隸｢扈捺棡
        $this->free();
        
        $this->QueryStr = $sql;
        $this->ptrQuery = mysql_query( $this->queryStr, $this->ptrLink );
        //$this->debug();
        if ( !$this->ptrQuery ) {
            return false;
        } 
        
        return $this->getResult();
    }
    
    public function getResult() {
        if ( !$this->ptrQuery ) {
            return false;
        }
        //霑泌屓謨ｰ謐ｮ髮�
        $result = array();
        if( mysql_num_rows( $this->ptrQuery ) > 0 ) {
            while( $row = mysql_fetch_assoc( $this->ptrQuery ) ) {
                $result[] = $row;
            }
            // 闔ｷ蜿門ｮ梧園髴�ｻ捺棡�悟ｰ�欠髓亥､榊次
            mysql_data_seek( $this->ptrQuery, 0 );
        }
        return $result;
    }

    public static function Instance( $DBConfigKey ) 
    {
		if ( self::$_instances[$DBConfigKey] == null ) {
			 $db = new DB( $DBConfigKey );
			 $db->connect();
			 self::$_instances[$DBConfigKey] = $db;
		}
		
		return self::$_instances[$DBConfigKey];
    }
    
    
    
}
?>